package com.atguigu.day06;

import com.atguigu.utils.ClickEvent;
import com.atguigu.utils.ClickSource;
import org.apache.flink.runtime.state.filesystem.FsStateBackend;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

public class Example1 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        // 每隔10秒钟保存一次检查点
        env.enableCheckpointing(10 * 1000L);
        // 设置保存路径，file:// + 文件夹的绝对路径
        env.setStateBackend(new FsStateBackend("file:///home/zuoyuan/flinktutorial0819/src/main/resources/ckpts"));

        env
                .addSource(new ClickSource())
                .keyBy(r -> r.username)
                .process(new KeyedProcessFunction<String, ClickEvent, ClickEvent>() {
                    @Override
                    public void processElement(ClickEvent value, Context ctx, Collector<ClickEvent> out) throws Exception {
                        out.collect(value);
                    }
                })
                .print();

        env.execute();
    }
}
